<?php
namespace app\api\controller\user;
use app\admin\model\user\User;
use app\common\annotation\ApiNodeAnnotation;
use app\common\controller\ApiBase;
use think\Request;

/**
 * @ApiNodeAnnotation(
 *     title="登录板块",
 *     parent="api/user.index",
 *     is_release=0,
 *     auth=1,
 * )
 */
class Login extends ApiBase
{
    public $model;

    public function initialize()
    {
        parent::initialize();
        $this->model = new User();
    }

    /**
     * @ApiNodeAnnotation(
     *     title="用户登录",
     *     parent="api/user.login",
     *     is_release=1,
     *     auth=1,
     * )
     */
    public function index(Request $request)
    {
        if($request->isPost())
        {
            $input = $request->post();

            // 检测输入的验证码是否正确，$value为用户输入的验证码字符串
            if(!cmf_captcha_check($input['verifyCode']))
            {
                $this->error("验证码错误",url('admin/login/index'));
            }

            $adminUser = $this->model->whereOr("username",$input['username'])
                ->whereOr("phone",$input['username'])
                ->whereOr("email",$input['username'])
                ->find();

            if($adminUser)
            {
                if(cmf_compare_password($adminUser['password'],$input['password']) )
                {
                    $loginTime = time();
                    $loginIp   = get_client_ip();
                    $loginData = [
                        "last_login_ip"   => $loginIp,
                        "login_ip"        => $loginIp,
                        "login_time"      => $loginTime,
                        "last_login_time" => $loginTime,
                    ];
                    //上次登陆ip
                    if(!$adminUser['login_ip'])
                    {
                        $loginData['last_login_ip'] = $adminUser['login_ip'];
                    }
                    //上次登录时间
                    if(!$adminUser['login_time'])
                    {
                        $loginData['last_login_time'] = $adminUser['login_time'];
                    }
                    //登录次数加 1
                    $this->model->whereOr("username",$adminUser['username'])
                        ->whereOr('phone',$adminUser['username'])
                        ->whereOr("email",$input['username'])
                        ->setInc("login_times",1);

                    $res = $this->model->whereOr("username",$adminUser['username'])
                        ->whereOr('phone',$adminUser['username'])
                        ->whereOr("email",$input['username'])
                        ->update($loginData);

                    //生成token
                    cmf_generate_user_token($adminUser["id"], 'web');
                    if($res)
                    {
                        //存储用户信息
                        $this->success('登录成功','',$adminUser);
                    }else{
                        $this->error('登录失败');
                    }
                }else{
                    $this->error("密码错误");
                }
            }else{
                $this->error("账户不存在");
            }
        }
    }

}